package at.jaki.ubb.crawling.stlib.dataFinder;

import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import at.jaki.ubb.crawling.api.DataFinder;
import at.jaki.ubb.crawling.model.DataFinderResult;
import at.jaki.ubb.crawling.model.RawData;
import at.jaki.ubb.crawling.model.Target;

public class SimpleHttpClient implements DataFinder<String, String, String> {

	/**
	 * will accept {@link Target}s of this type
	 */
	public static final String INPUT_TYPE = "complex-url";
	/**
	 * will create {@link RawData} of this type
	 */
	public static String RESULT_TYPE = "webPage";

	private static final Logger LOG = Logger.getLogger(SimpleHttpClient.class);

	@Override
	public DataFinderResult<String, String> findData(Target<String> t) {

		LOG.info("getting page: " + t.getValue());
		
		String page = getPage(t.getValue());

		if (page != null) {
			RawData<String> rd = new RawData<String>(t.getValue(), page, new Date(), RESULT_TYPE);
			Collection<RawData<String>> rawDataCollection = Collections.singletonList(rd);
			return new DataFinderResult<String, String>(rawDataCollection, null);
		} else {
			return new DataFinderResult<String, String>(null, null);
		}
	}

	protected String getPage(String uri) {
		LOG.debug("getting page: " + uri);
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(uri);
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				try {
					return new Scanner(instream).useDelimiter("\\A").next();
				} finally {
					instream.close();
				}
			}

		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public void init() {

	}

	@Override
	public Collection<String> getTypes() {
		return Collections.singletonList(INPUT_TYPE);
	}

}
